package com.gitee.feizns.quickstart.ding.talk.api.v10.device.mng.customers.devices.activations;

import com.aliyun.dingtalkdevicemng_1_0.Client;
import com.aliyun.dingtalkdevicemng_1_0.models.ListActivateDevicesHeaders;
import com.aliyun.dingtalkdevicemng_1_0.models.ListActivateDevicesRequest;
import com.aliyun.dingtalkdevicemng_1_0.models.ListActivateDevicesResponse;
import com.aliyun.dingtalkdevicemng_1_0.models.ListActivateDevicesResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.gitee.feizns.quickstart.ding.talk.client.Clients;
import com.gitee.feizns.quickstart.ding.talk.utils.Utils;
import lombok.NonNull;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询已经注册的设备信息
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/query-information-about-a-registered-device">查询已经注册的设备信息（/v1.0/devicemng/customers/devices/activations/infos）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/query-information-about-a-registered-device">服务端API 行业开放 设备上钉 查询已经注册的设备信息</a>
 * @author feizns
 * @since 2024/3/18
 */
public class Infos {

    /**
     * 查询已经注册的设备信息
     * @param accessToken 访问凭据
     * @param req {@link ListActivateDevicesRequest}
     */
    @SneakyThrows
    public static ListActivateDevicesResponseBody execute(@NonNull String accessToken,
                                                          @NonNull ListActivateDevicesRequest req) {
        Client client = Clients.create(Client.class);
        ListActivateDevicesHeaders listActivateDevicesHeaders = new ListActivateDevicesHeaders();
        listActivateDevicesHeaders.xAcsDingtalkAccessToken = accessToken;
        ListActivateDevicesResponse resp = client.listActivateDevicesWithOptions(req, listActivateDevicesHeaders, new RuntimeOptions());
        return Utils.ifErrorThrow(resp.getBody());
    }


    /**
     * 查询已经注册的设备信息
     * @param accessToken 访问凭据
     * @param deviceCategory 设备分类（0=设备、1=助手）
     * @param deviceCode 设备编号。
     * @param deviceTypeId devicetype编号
     * @param groupId group编号
     * @return {@link List<ListActivateDevicesResponseBody.ListActivateDevicesResponseBodyResult>}
     */
    @SneakyThrows
    public static List<ListActivateDevicesResponseBody.ListActivateDevicesResponseBodyResult> execute(@NonNull String accessToken,
                                                          Integer deviceCategory,
                                                          String deviceCode,
                                                          String deviceTypeId,
                                                          String groupId) {
        ListActivateDevicesRequest request = new ListActivateDevicesRequest();
        request.setDeviceCategory(deviceCategory);
        request.setDeviceCode(deviceCode);
        request.setDeviceTypeId(deviceTypeId);
        request.setGroupId(groupId);
        int size = 50;
        int no = 1;
        List<ListActivateDevicesResponseBody.ListActivateDevicesResponseBodyResult> result = new ArrayList<>();
        ListActivateDevicesResponseBody resp;
        do {
            request.setPageNumber(no);
            request.setPageSize(size);
            resp = execute(accessToken, request);
            result.addAll(resp.getResult());
        } while ( (long) no++ * size < resp.getTotalCount());
        return result;
    }

}
